package com.andnnl.single;

/**
 * 真真的延迟加载单例
 * Created by chenss on 2020/8/31.
 */
public class Singleton1 {
    private String name;

    private Singleton1() {
    }

    private Singleton1(String name) {
        this.name = name;
        System.out.println("init.." + name);
    }

    public static class Holder {
        // 这里的私有没有什么意义
        /* private */static Singleton1 instance = new Singleton1("test1");
    }

    public static class Holder2 {
        // 这里的私有没有什么意义
        /* private */static Singleton1 instance = new Singleton1("test2");
    }

    public static Singleton1 getInstance() {
        // 外围类能直接访问内部类（不管是否是静态的）的私有变量
        return Holder.instance;
    }

    public static Singleton1 getInstance2() {
        // 外围类能直接访问内部类（不管是否是静态的）的私有变量
        return Holder2.instance;
    }


    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        System.out.println(getInstance());
        System.out.println(getInstance2());
        System.out.println(getInstance());
        System.out.println(getInstance2());
    }
}
